<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="湖库" prop="status">
        <el-input v-model="queryParams.lakename" placeholder="请输入湖库" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain 
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          @click="handleUpdate"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          @click="handleDelete()"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table  :data="lakeList" style="width: 100%"  v-if="refreshTable" v-loading="loading" @selection-change="handleSelectionChange">
        <el-table-column  type="selection" width="55" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column  prop="lakename" label="湖库名称" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column  prop="longitude" label="经度" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column  prop="latitude" label="维度" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column  prop="lakearea" label="湖泊总面积（km²）" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column  prop="waterdepth" label="平均水深（m）" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
            >删除</el-button>

          </template>
        </el-table-column>
    </el-table>
     <el-pagination layout="total,sizes,prev, pager, next" :total="this.total" background :page-size="10"  :page-sizes="[10, 20, 50]"  @size-change="handleSizeChange" @current-change="handleCurrentChange"></el-pagination>
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form"label-width="140px" :rules="rules">
        <el-row>
          <el-col :span="12">
            <el-form-item label="湖库名称" prop="lakename">
              <el-input v-model="form.lakename" placeholder="请输入湖库名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="湖库别名" prop="lakealias">
              <el-input v-model="form.lakealias" placeholder="请输入湖库别名" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="湖库编码" prop="lakecode">
              <el-input v-model="form.lakecode" placeholder="请输入湖库编码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属流域" prop="firstriversystem">
              <el-select v-model="form.firstriversystem" placeholder="" style="width:100%">
                  <el-option v-for="item in riverBasinList" :key="item.id" :label="item.basinname" :value="item.basinname">
                  </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否跨行政区" prop="isspanned">
              <el-radio v-model="form.isspanned" label="1">是</el-radio>
              <el-radio v-model="form.isspanned" label="0">否</el-radio>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所跨行政区域" prop="region">
              <el-select v-model="form.region" placeholder="" style="width:100%"  @change="formChange($event,'code',{'regionname':'name'},regionList)">
                  <el-option v-for="item in regionList" :key="item.code" :label="item.name" :value="item.code">
                  </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="经度" prop="longitude">
              <el-input v-model="form.longitude" placeholder="请输入湖库经度" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="纬度" prop="latitude">
              <el-input v-model="form.latitude" placeholder="请输入湖库纬度" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="管理单位" prop="managerunit">
              <el-input v-model="form.managerunit" placeholder="请输入湖库管理单位" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否启用" prop="lifecycle">
              <el-radio v-model="form.lifecycle" label="1">是</el-radio>
              <el-radio v-model="form.lifecycle" label="0">否</el-radio>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="湖库/水库等级" prop="lakegradecode">
              <el-select v-model="form.lakegradecode" placeholder="" style="width:100%"  @change="formChange($event,'dictCode',{'lakegradename':'dictLabel'},lakeLevelList)">
                <el-option v-for="item in lakeLevelList" :key="item.dictCode" :label="item.dictLabel" :value="item.dictCode">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="湖泊总面积" prop="firstriversystem">
              <el-input type="number" v-model="form.lakearea" placeholder="" >
                 <template slot="append">（km²）</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="平均水深" prop="waterdepth">
              <el-input type="number" v-model="form.waterdepth" placeholder="" >
                 <template slot="append">（m）</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="湖岸线长度" prop="lakelength">
              <el-input type="number" v-model="form.lakelength" placeholder="" >
                 <template slot="append">（km）</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="岸线开发利用长度" prop="uselength">
              <el-input type="number" v-model="form.uselength" placeholder="" width="60">
                 <template slot="append">（m）</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="湖泊限制排污总量" prop="limitnum">
              <el-input type="number" v-model="form.limitnum" placeholder="" width="60">
                 <template slot="append">(万m³/年)</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="库容" prop="capacity">
              <el-input type="number" v-model="form.capacity" placeholder="" width="60">
                 <template slot="append">(万m³/年)</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
             <el-form-item label="简介" prop="capacity">
               <el-input type="textarea" v-model="form.introduction" placeholder=""  maxlength="200" show-word-limit></el-input>
             </el-form-item>
          </el-col>
        </el-row>
        <!-- <el-row >
          <el-col :span="12">
            <el-form-item label="图片">
              <el-upload
                class="avatar-uploader"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload">
                <img v-if="imageUrl" :src="imageUrl" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {basinList} from '@/api/dataResources/monitorData/riverbasin.js';
import {getRegionList} from '@/api/dataResources/monitorData/region.js'
import {getDicts} from '@/api/system/dict/data.js';
import {getLakeList,lakeAdd,lakeEdit,lakeRemove} from '@/api/dataResources/monitorData/lake.js'
export default {
  data(){
    return {
      showSearch: true,
      queryParams:{
        lakename:'',
        pageSize:10,
        pageNum:1
      },
      lakeList:[],
      lakeLevelList:[],
      riverBasinList:[],
      regionList:[],
      tableSelect:[],
      refreshTable:true,
      loading: false,
      title:'',
      total:0,
      open:false,
      form: {},
      rules:{
        lakename:[
          { required: true, message: "湖库名称不能为空", trigger: "blur" }
        ],
        lakecode:[
          { required: true, message: "湖库代码不能为空", trigger: "blur" }
        ],
        lifecycle:[
          { required: true, message: "是否启用不能为空", trigger: "blur" }
        ],
        lakegradecode:[
          { required: true, message: "湖库等级不能为空", trigger: "blur" }
        ],
        lakegradename:[
          { required: true, message: "湖库等级名称不能为空", trigger: "blur" }
        ],
      }
    }
  },
  created() {
    this.getRiverBasinList();
    this.getLakeLevelList();
    this.getRegionList();
    this.getList();
  },

  methods: {
    dateFormat(val){
      return this.parseTime(val,'{yyyy}-{mm}-{dd} {hh}:{ii}:{ss}')
    },
    handleQuery() {
      this.getList();
    },
    handleAdd() {
      this.open = true;
      this.title = "添加湖库信息";
      this.form = {
        "populationcovered": "",
        "secondriversystem": "",
        "firstriversystem": "",
        "firstriversystemcode": "",
        "thirdriversystem": "",
        "runoffcapacity": "12.",
        "secondriversystemcode": "",
        "thirdriversystemcode": "",
        "longitude": "",
        "latitude": "",
        "region": "",
        "rivertsort": "",
        "basinarea": "",
        "lakearea": "",
        "introduction": "",
        "lakename": "",
        "lakecode": "",
        "regionname": "",
        "imagesurl": "",
        "uselength": "",
        "lakealias": "",
        "isspanned": "",
        "waterdepth": "",
        "watercapacity": "",
        "lakelength": "",
        "limitnum": "",
        "capacity": "",
        "box": null,
        "lifecycle": "1",
        "lakegradecode": "",
        "managerunit": "",
        "lakegradename": ""
      }
    },
    formChange(value,key,map,list){
      let item = list.find(it => it[key]==value);
      if(item) {
        for(let fkey in map){
          this.form[fkey] = item[map[fkey]]
          console.log('fkey',fkey,this.form[fkey])
        }
      }
    },
    handleSizeChange(val){
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val){
      this.queryParams.pageNum = val;
      this.getList();
    },
    // formChange(value,key,list){
    //   console.log(value,key)
    //   if(key == 'firstriversystemcode'){
    //     let item = list.find(item => item.id == value);
    //     this.form.firstriversystem = item.basinname;
    //   } else if(key == 'lakegradename'){
    //      let item = list.find(item => item.dictLabel == value);
    //      this.form.lakegradecode = item.dictCode;
    //   }
    // },
    handleUpdate(row) {
      this.open = true;
      this.title = "编辑湖库";
      this.form = {...row};

    },
    async handleDelete(row = null) {
      let id;
      if(row){
        id = row.id;
      } else if(this.tableSelect.length > 0){
        id = this.tableSelect.map(item => item.id).join(',');
      }
      await lakeRemove(id);
      this.getList();
    },
    handleSelectionChange(val){
      this.tableSelect = val;
    },
    handleExport() {

    },
    resetQuery() {
      this.queryParams = {
        lakename: '',
        pageSize: 10,
        pageNum: 1
      };
      this.getList();
    },
    submitForm(){
      this.$refs["form"].validate((valid) =>{
        if(valid){
          this.saveByApi();
        }
      })
    },
    async saveByApi(){
      this.open = false;
      if(this.title == "添加湖库信息"){ 
        await lakeAdd(this.form);
      } else {
        await lakeEdit(this.form);
      }
      this.getList();
    },
    cancel() {
      this.open = false;
    },
    async getList() {
      this.tableSelect = [];
      this.loading = true
      let res = await getLakeList(this.queryParams);
      if(res.code =='200') {
        this.lakeList = res.rows;
        this.total = res.total;
      }
      this.loading = false;
    },
        async getRiverBasinList() {
      let res = await basinList();
      if(res.code == 200){
        this.riverBasinList = res.rows;
      }
    },
    async getLakeLevelList() {
      let res = await getDicts('lake_grade');
      if(res.code == 200){
        this.lakeLevelList = res.data;
      }
    },
    async getRegionList() {
      let res = await getRegionList();
      if(res.code == 200){
        this.regionList = res.rows;
      }
    },
  },
};
</script>

<style></style>
